#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

get_pid() {
	pidof /usr/bin/ircd
}

case "$1" in
  start)
    stat_busy "Starting Ultimate IRCd"

    [ -d /var/run/ircd ] || mkdir -p /var/run/ircd
    [ -f /var/run/ircd/ircd.pid ] && rm -f /var/run/ircd/ircd.pid
    PID=`get_pid`
    if [ -z "$PID" ]; then
      su -c '/usr/bin/ircd -f /etc/ircd/ircd.conf' ircd 1>>/var/log/ircd/stdout.log 2>>/var/log/ircd/sterr.log
      if [ $? -gt 0 ]; then
        stat_fail
        exit 1
      else
        add_daemon ultimate-ircd
        stat_done
      fi
    else
      stat_fail
      exit 1
    fi
    ;;

  stop)
    stat_busy "Stopping Ultimate IRCd"
    PID=`get_pid`
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
      exit 1
    else
      rm -f /var/run/ircd/ircd.pid &> /dev/null
      rm_daemon ultimate-ircd
      stat_done
    fi
    ;;

  restart)
    $0 stop
    sleep 3
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
